package com.lee.carinforecord;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class CarBroadcastReceiver extends BroadcastReceiver {
    public String[] canDataStrArr = null;   //can总线数据

    @Override
    public void onReceive(Context context, Intent intent) {

        InfoWindow.show(context);

        byte[] originalByte = (byte[]) intent.getExtras().get("canbus_data");

        if (originalByte.length > 0) {
            String dataStr = CanDataTool.byteToHex(originalByte);
            Log.i("can data", dataStr + "");
            this.canDataStrArr = CanDataTool.f(dataStr); //分割字符串获得数组

            String s1 = getMileage();
            String s2 = getConsumption();
            String s3 = getAvailableMile();

            if (!s1.equals("") && !s2.equals("") && !s3.equals("")) {
                DataSave.availableMile = Integer.parseInt(s3);
                DataSave.currentMile = Float.parseFloat(s1);
                DataSave.currentOil = Float.parseFloat(s2);
                //DataSave.save(context);
                InfoWindow.update(s3, s1, s2);
                FileTools.saveInfo(s3+","+s1+","+s2);
            }

            if(MainActivity.getInstance() != null){
                MainActivity.getInstance().updateEditView(dataStr + "\n");
                MainActivity.getInstance().updateEditView(s1 + ", " + s2 + ", " + s3 + "\n");
            }
        }
    }

    public String getMileage() {
        String result = "";
        if (CanDataTool.d(CanDataTool.a(this.canDataStrArr, 0)) == 1) { //第0位 如果是1，则继续
            int s1 = CanDataTool.d(CanDataTool.a(this.canDataStrArr, 8));
            int s2 = CanDataTool.d(CanDataTool.a(this.canDataStrArr, 9));
            int s3 = CanDataTool.d(CanDataTool.a(this.canDataStrArr, 10));
            String val = CanDataTool.a(s1, s2, s3);
            result = val; // + " km";
            Log.i("里程", result);//里程
        }
        return result;
    }

    public String getConsumption() {
        String result = "";
        if (CanDataTool.d(CanDataTool.a(this.canDataStrArr, 0)) == 1) { //第0位 如果是1，则继续
            double val = CanDataTool.a(CanDataTool.d(CanDataTool.a(this.canDataStrArr, 6)), CanDataTool.d(CanDataTool.a(this.canDataStrArr, 7)));
            if (val > 0.0d && val < 65535.0) {
                result = val / 10 + "";// + " l/100km";
                Log.i("油耗", result);//油耗
            }
        }
        return result;
    }

    public String getAvailableMile() {
        String result = "";
        //Log.i("test", CanDataTool.a(this.canDataStrArr, 11));
        //Log.i("test", CanDataTool.a(this.canDataStrArr, 12));
        int val = (CanDataTool.d(CanDataTool.a(this.canDataStrArr, 11)) * 256) + CanDataTool.d(CanDataTool.a(this.canDataStrArr, 12));
        if (val > 0 && val < 65535) {
            result = val + ""; // + " km";
            Log.i("可行驶", result);//油耗
        }
        return result;
    }
}